package com.gosophia.metadataEngine.service.impl.test.integration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import com.gosophia.commons.OrderCondition;
import com.gosophia.commons.Page;
import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.metadataEngine.service.BusinessDataService;
/**
 * BusinessDataService集成测试
 * @author wangxinbing
 * @since 2011-4-30
 *
 */
@ContextConfiguration(locations = { "classpath:applicationContext-it.xml",
"classpath*:moduleContext.xml" })
public class ITBusinessDataServiceImpl extends
        AbstractTransactionalJUnit4SpringContextTests {
    @Autowired
    private BusinessDataService  businessDataService;
    @Test
    public void testFindSystemDataDetail(){
        Map<String, Object> map = businessDataService.findSystemDataDetail(1L,6L);
        Assert.assertEquals(5, map.size());
    }
    //高级搜索
    @Test
    public void testFindSystemDataAdvanceByPage(){
        PropertyFilter propertyFilter = new PropertyFilter("sex.id", 1L);
        List<PropertyFilter> ps = new ArrayList<PropertyFilter>();
        ps.add(propertyFilter);
        List<OrderCondition> ords = new ArrayList<OrderCondition>();
        OrderCondition ord = new OrderCondition("name",false);
        ords.add(ord);
    	Page<Map<String,Object>> page = new Page<Map<String,Object>>();
    	page.setPageNo(1);
    	page.setPageSize(10);
        page = businessDataService.findSystemDataAdvanceByPage(6L, ps, page, ords);
        Assert.assertEquals(8, page.getTotalCount());
    }
    //模糊搜索
    @Test
    public void testFindSystemDataSimpleByPage(){
    	Page<Map<String,Object>> page = new Page<Map<String,Object>>();
    	page.setPageNo(1);
    	page.setPageSize(10);
    	List<OrderCondition> ords = new ArrayList<OrderCondition>();
        OrderCondition ord = new OrderCondition("name",false);
        ords.add(ord);
        page = businessDataService.findSystemDataSimpleByPage(6L, page, "u", ords, null);
        Assert.assertEquals(15, page.getTotalCount());
    }
    //手动初始化sessionFactories
    @Test
    public void testInitMetadataSessionFactories(){
    	businessDataService.initMetadataSessionFactories();
    }
    @Test
    public void testFindRefValues() throws BusinessException{
    	System.out.println(businessDataService.findRefValues(6l, 8L));
    	for(List o : businessDataService.findRefValues(6l, 8L)){
    		System.out.println(o.get(0));
    	}
    }
    /*@Rollback(false)
    @Test
    public void testDeleteSystemDataById() throws BusinessException{
    	businessDataService.deleteSystemDataById(1L, 6L);
    }
    @Rollback(false)
    @Test
    public void testUpdateSystemData() throws BusinessException{
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("name", "王2");
    	map.put("sex",2L);
    	businessDataService.updateSystemData(1L, 6l, map);
    }
    @Rollback(false)
    @Test
    public void testAddSystemData() throws BusinessException{
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("name", "王4");
    	Map<String,Object> sex = new HashMap<String, Object>();
    	sex.put("id",2L);
        map.put("sex",2L);
    	businessDataService.addSystemData(6l, map);
    }*/
}
